package com.crm.controller.Registration;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.BusinessDAO;
import com.crm.dao.UserDAO;
import com.crm.entity.Business;
import com.crm.entity.User;
import com.crm.util.EncriptAndDecript;

@Controller
@RequestMapping("/Register")
public class RegisterController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest request,ModelAndView model) throws Exception{
		return model=new ModelAndView("/jsp/Register.jsp")
		.addObject("submenu", "admin")
		.addObject("menuName","admin");
	}
	@RequestMapping("/register.do")
	public ModelAndView register(HttpServletRequest request,ModelAndView model,HttpSession session) throws Exception{
		String compname=(String) request.getParameter("businessName");
		String address=(String) request.getParameter("address");
		String tinNo=(String) request.getParameter("tinNo");
		String cstNo=(String) request.getParameter("cstNo");
		String phoneNo=(String) request.getParameter("phone");
		String panNo=(String) request.getParameter("panNo");
		String shortName=(String) request.getParameter("shortName");
		String fax=(String) request.getParameter("fax");
		String sifsc=(String) request.getParameter("ifsc");
		String email=(String) request.getParameter("email");
		String emailpwd=(String) request.getParameter("emailpwd");
		String accountNo=(String) request.getParameter("accountNo");
		String micrNo=(String) request.getParameter("micr");
		String terms=(String) request.getParameter("tAndC");

		

		byte[] encrypted=EncriptAndDecript.encrypt(emailpwd);
		System.out.println("Encrypted Email Pwd Is:"+encrypted);
		
		String userName=(String) request.getParameter("adminUName");
		String password=(String) request.getParameter("adminPwd");
		String firstName=(String) request.getParameter("adminFName");
		String lastName=(String) request.getParameter("adminLName");
		
		byte[] logo=null;
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("logo");
		
		
		logger.info("ContentType:" + file.getContentType());
		try {
			  logo=file.getBytes();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		long accno=Long.parseLong(accountNo);
		long ifsc=Long.parseLong(sifsc);
		
		BusinessDAO busDAO=new BusinessDAO();
		Business bus=new Business(compname,address,tinNo,cstNo,phoneNo,panNo,shortName,fax,micrNo,email,emailpwd,accno,ifsc,terms,logo);
		
		model=new ModelAndView("/jsp/index.jsp");
		UserDAO uDAO=new UserDAO();
		User u=new User(bus, userName, password, firstName,lastName,email, emailpwd);
		try{
			busDAO.insert(bus);
			uDAO.insert(u);
			model.addObject("msg","Business Is Successfully Regsitered.");
		}catch(Exception e){
			e.printStackTrace();
			model.addObject("msg","Failed To Register The Business.Please Try Again.");
		}
		return model=model.addObject("submenu", "admin")
		.addObject("menuName","admin");
	}
	
}
